package com.reggie.ExceptionHandler;

import com.reggie.Exception.CategoryException;
import com.reggie.Exception.DishStatusException;
import com.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLIntegrityConstraintViolationException;

@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
@Slf4j
public class Handler1 {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> globalExceptionHandler1(SQLIntegrityConstraintViolationException ex) {
        System.out.println(ex.getMessage());
        return R.error(ex.getMessage().split(" ")[2] + "已经存在了！添加失败！");
    }

    //菜品种类异常捕获器
    @ExceptionHandler(CategoryException.class)
    public R<String> globalExceptionHandler2(CategoryException ex){
        log.info(ex.getMessage());
        return R.error(ex.getMessage());
    }

    //添加商品状态异常捕获器
    @ExceptionHandler(DishStatusException.class)
    public R<String> globalExceptionHandler3(DishStatusException ex){
        log.info(ex.getMessage());
        return R.error(ex.getMessage());
    }
}
